/*
 * MirroringForm.java
 *
 * Created on 14 
 */
package gui;

import data.GeneralDefinitions.DiskType;
import java.awt.Frame;
import javax.swing.JOptionPane;
import main.ToEngineFacade;

/**
 *
 * @author  Elad
 */
public class MirroringForm extends javax.swing.JFrame {

    private Double bufferSize = null;
    private ToEngineFacade toEngineFacade = null;
    private Frame parentFrame;

    /** Creates new form MirroringForm */
    public MirroringForm(Frame frame, ToEngineFacade toEngFacade) {
        initComponents();
        toEngineFacade = toEngFacade;
        parentFrame = frame;

    //default values for params
    //  SSDRadioButton.setSelected(true);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        OK_jButton = new javax.swing.JButton();
        back_jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        bufferSizeTextField = new javax.swing.JTextField();
        SCASIRadioButton = new javax.swing.JRadioButton();
        SSDRadioButton = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(3, 141, 187));

        OK_jButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        OK_jButton.setForeground(new java.awt.Color(57, 82, 148));
        OK_jButton.setText("OK");
        OK_jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OK_jButtonActionPerformed(evt);
            }
        });

        back_jButton1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        back_jButton1.setForeground(new java.awt.Color(57, 82, 148));
        back_jButton1.setText("Back");
        back_jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                back_jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("Buffer Size");

        bufferSizeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bufferSizeTextFieldActionPerformed(evt);
            }
        });

        SCASIRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(SCASIRadioButton);
        SCASIRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        SCASIRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        SCASIRadioButton.setText("SCSI");
        SCASIRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SCASIRadioButtonActionPerformed(evt);
            }
        });

        SSDRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(SSDRadioButton);
        SSDRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        SSDRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        SSDRadioButton.setText("SSD");
        SSDRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SSDRadioButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("Type of Disk");

        jLabel1.setFont(new java.awt.Font("Bodoni MT Black", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(227, 186, 196));
        jLabel1.setText("Parameters for Mirroring algorithm");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SCASIRadioButton)
                    .addComponent(bufferSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SSDRadioButton))
                .addContainerGap(308, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(354, Short.MAX_VALUE)
                .addComponent(back_jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OK_jButton)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(SSDRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SCASIRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(bufferSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OK_jButton)
                    .addComponent(back_jButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SCASIRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SCASIRadioButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_SCASIRadioButtonActionPerformed

private void SSDRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SSDRadioButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_SSDRadioButtonActionPerformed

private void bufferSizeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bufferSizeTextFieldActionPerformed
}//GEN-LAST:event_bufferSizeTextFieldActionPerformed

private void OK_jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OK_jButtonActionPerformed
//    System.out.println(" OK Botton clicked ");

    //   boolean isAllParamsOk = true;
    DiskType typeOfDisk = null; //0=Read, 1 = write

    //type of disk
    if (SSDRadioButton.isSelected()) {
        typeOfDisk = DiskType.SSD;
    } else if (SCASIRadioButton.isSelected()) {
        typeOfDisk = DiskType.SCSI;
    } else {
        JOptionPane.showConfirmDialog(
                null,
                "Type of Disk must be a choosen!",
                "Parameter missing error",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.OK_OPTION);
        //  isAllParamsOk = false;
        return;
    }
    //buffer size
    try {
        if (!bufferSizeTextField.getText().equals("")) {
            bufferSize = new Double(bufferSizeTextField.getText());
        }
        else bufferSize = null;
    } catch (NumberFormatException numberFormatException) {
        System.out.println("  Buffer size mustbe an int !");
        JOptionPane.showConfirmDialog(
                null,
                "Buffer size must be a number!",
                "Parameter type error",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.OK_OPTION);
        bufferSizeTextField.setText(null);
        bufferSizeTextField.requestFocus();
        //    isAllParamsOk = false;
        return;
    }
    //   if (isAllParamsOk) {
    //this.setVisible(false);
    toEngineFacade.computeMirroring(typeOfDisk, bufferSize);

/*} else {
JOptionPane.showConfirmDialog(
null,
"You cannot continue if not all parameters are entered correctly!",
"Missing parameters error",
JOptionPane.DEFAULT_OPTION,
JOptionPane.OK_OPTION);
bufferSizeTextField.requestFocus();
}*/
}//GEN-LAST:event_OK_jButtonActionPerformed

private void back_jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_jButton1ActionPerformed
    parentFrame.setVisible(true);
    this.dispose();
}//GEN-LAST:event_back_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new MirroringForm().setVisible(true);
    }
    });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton OK_jButton;
    private javax.swing.JRadioButton SCASIRadioButton;
    private javax.swing.JRadioButton SSDRadioButton;
    private javax.swing.JButton back_jButton1;
    private javax.swing.JTextField bufferSizeTextField;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
